<?php
defined( 'IN_DICE' ) or die( 'Access Error!' );

include DIR_MDL . 'k.php'; //加载KEY生成文件

/**
 * 全局变量类
 */
class all {
  
  public static $cfg = array();
  public static $aU = array();
  public static $aOut = array();
  public static $langflag = '';
  
  public static function setcfg( $cfg ) {
    self::$cfg = array_merge( self::$cfg, $cfg );
  }

  /**
   * 设置语言文件
   *
   * @param int $langtype 语言类型,查看 cfg.game 获得详情
   * @return bool
   */
  public static function loadlang( $langtype ) {
    $langtype = uint( $langtype );
    if( !in_array( $langtype, array_keys( all::$cfg['language'] ) ) ) {
      return false;
    }
    $langflag = all::$cfg['language'][$langtype];
    $langclass = 'lang_' . $langflag;
    $langfile = DIR_VIEW . 'lang/lang_' . $langflag . '.php';
    if( !class_exists( $langclass ) && file_exists( $langfile ) ) {
      self::$langflag = $langflag;
      include_once $langfile;
      return true;
    }
    return false;
  }
  
}

class base {
  
  public $usergame = 'usergame'; //用户游戏数据表
  public $userinfo = 'userinfo'; //用户平台数据表
  public $uother = 'uother'; //用户扩展信息表
  public $online = 'online'; //在线表
  public $server = 'server'; //服务器表
  public $ttable = 'ttable'; //桌子表
  public $chiplog = 'chiplog'; //游戏币日志
  public $tablelog = 'tablelog'; //游戏详情日志表
  public $winlog = 'winlog'; //玩家输赢日志
  
  function __construct() {
    $aV = get_class_vars( 'base' );
    foreach ( $aV as $k => $v ) {
      if( in_array( $v, array( 'offline' ) ) ) {
        $this->$k = all::$cfg['db']['offline']['dbname'] . '.' . $v;
      } else if( in_array( $v, array( 'chiplog', 'tablelog', 'winlog', ) ) ) {
        $this->$k = all::$cfg['db']['log']['dbname'] . '.' . $v;
      } else {
        $this->$k = all::$cfg['db']['user']['dbname'] . '.' . $v;
      }
    }
  }
  
  /**
   * 得到用户离线表,用于错开写入与回写
   *
   * @param int $flag
   */
  public function tbloffline( $flag = 0 ) {
    $minutes = date( 'i' );
    return $this->offline . ( $flag ? ( $minutes + 1 ) % 2 : $minutes % 2 );
  }
  
  /**
   * 获得用户的好友关系所在的数据表
   * @param int $uid
   * @return string
   */
  public function tblfriend( $uid ) {
    $uid = uint( $uid );
    $friend = $this->friend . ( $uid % 100 );
    return $friend;
  }

  /**
   * 获得用户的礼物所在的数据表
   * @param int $uid
   * @return string
   */
  public function tblgift( $uid ) {
    $uid = uint( $uid );
    $tbl = $this->ugifts . ( $uid % 100 );
    return $tbl;
  }
}
